//
//  HomeHeadView.m
//  ai2ban
//
//  Created by Luolc on 2018/1/9.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "HomeHeadView.h"
#import <SDCycleScrollView.h>
#import "UIButton+YSJBTN.h"

@interface HomeHeadView()<SDCycleScrollViewDelegate>

@property (nonatomic,strong)SDCycleScrollView *bannerView;

@end

@implementation HomeHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initTheUI];
    }
    return self;
}

- (void)setData:(NSArray *)data {
    _data = data;
    NSMutableArray *imageUrls = [[NSMutableArray alloc]init];
    for (NSDictionary *imgData in data) {
        NSString *imgUrl = [NSString stringWithFormat:@"%@%@", ImageBaseUrl, imgData[@"picImg"]];
        [imageUrls addObject:imgUrl];
    }
    self.bannerView.imageURLStringsGroup = imageUrls;
}

- (void)initTheUI {
    
    self.backgroundColor = [UIColor whiteColor];
    NSArray   *imageS = @[[UIImage imageNamed:@"jiFenBgImage"]];
    self.bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreen_Width, 194) imagesGroup:imageS];
    self.bannerView.placeholderImage = ImageNamed(@"placeholderImage");
    self.bannerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.bannerView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    [self addSubview:self.bannerView];
    
    CGFloat width = 38;
    
    CGFloat height = width+20+10;
    
    CGFloat vMargin = 8;
    
    CGFloat leftMargin = 38;
    
     CGFloat hMargin = (kScreen_Width-width*3-leftMargin*2)/2.0;
    
    
    NSArray *titles = @[@"美食",@"购物",@"娱乐",@"酒店",@"免费",@"途伴"];
    
    NSArray *icons = @[@"meishi",@"gouwu",@"wule",@"jiudian",@"图层-47",@"旅游-(1)",];
    
    for (int i=0; i<titles.count; i++) {
        
        CGFloat x  = leftMargin + i%3*(hMargin+width);
        
        CGFloat y = vMargin + i/3*(vMargin+height)+self.bannerView.height;
        
        CGRect frame = CGRectMake(x, y, width, height);
        
        UIButton *btn = [UIButton createHomeBtnWithFrame:frame andTitle:titles[i] andIcon:icons[i]];
        [btn addTarget:self action:@selector(didBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        [self addSubview:btn];
        
    }
    
}

- (void)didBtnClick:(UIButton *)sender {
    
    if (self.didOptionButtonClick) {
        
        self.didOptionButtonClick(sender.tag);
    }
}


-(void)reloadBannerWithData:(id)data {
    
  
    
}

#pragma mark - SDCycleScrollViewDelegate
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView 
   didSelectItemAtIndex:(NSInteger)index {
    
}
@end
